// pages/signUp/signUp.js
let util = require("../../utils/util.js");
let baseImgUrl = util.baseImgUrl;
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    networkStatus: true,
    loadingStatus: false,//默认加载完成，true--正在加载中
    loadgif: util.imgUrl + "loading.gif",//加载gif图
    loading: util.imgUrl + "loading.png",
    none_network: util.imgUrl + "none_network.png",

    phoneflag:false,//测试用，判断用户是否绑定了手机号,实际为后台传递的值
    phonenum:"",
    greyshow:false,
    huodongid:"",
    genderlist:[{
      id:0,
      name:"男"
    },{
      id:1,
      name:"女"
    }],
    index:"",
    gender:"",
    genderText:"",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      //获取活动的id;
      //测试数据
      var that=this;
      var huodongid="123456";
    /*********判断网络状态***************** */
    wx.getNetworkType({
      success: function (res) {
        var networkType = res.networkType;
        if (networkType == "none" || networkType == "unknown") {
          that.setData({
            networkStatus: false
          })
        } else {
          that.setData({
            networkStatus: true,
            huodong_id: options.huodong_id,
          })
        }
      }, fail: function (err) {
        console.log("获取网络失败" + err);
        that.setData({
          networkStatus: false
        })
      }
    })
    //测试用
    that.setData({
      loadingStatus: true
    })
    setTimeout(function () {
      that.setData({
        loadingStatus: false
      })
    }, 500)

      that.setData({
        huodongid:huodongid
      })


  },
/************选择性别************* */
  choseGender:function(){
      var that=this;
     that.setData({
       greyshow:true
     })
  },

  /*********选择男********* */
  gender_male:function(){
    var that=this;
    that.setData({
      index:0,
      greyshow: false
    })

  },

  /*********选择女********* */
  gender_female: function () {
    var that = this;
    that.setData({
      index: 1,
      greyshow: false

    })
  },
  /*********取消性别选择********** */
  cancelGender:function(){
    var that=this;
    that.setData({
      greyshow:false

    })
  },
  // 邮箱验证部分

  inputemail: function (e) {

    let email = e.detail.value;
    console.log("------------"+email)

    let checkedNum = this.checkEmail(email)

  },

  checkEmail: function (email) {

    let str = /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/

    if (str.test(email)) {

      return true

    } else {

      wx.showToast({

        title: '请填写正确的邮箱号',

        icon:"none"

      })

      return false

    }

  },

//获取用户手机号
  getPhoneNumber: function (e) {
    var that=this;
    console.log("--------------e.detail--------------");
    console.log(e);
    console.log(e.detail.errMsg)
    console.log(e.detail.iv)
    console.log(e.detail.encryptedData)
    var encryptedData=e.detail.encryptedData;

    if (e.detail.errMsg == 'getPhoneNumber:user deny' || e.detail.errMsg ==  'getPhoneNumber:fail user deny') {
      wx.navigateBack({
        delta: 1
      })
      
    } else {
      var phone = encodeURIComponent(e.detail.encryptedData)
      //     console.log(phone);
      //测试数据，假设获取了解密后的手机号
          that.setData({
            phoneflag: true,
            phonenum: "12345678910"
          })
      
    }
  }  ,

    /*********表单验证*********** */
  formSubmit:function(e){
    var that=this;
      console.log(e);
      var nickname=e.detail.value.nickname;
      var gender_chosed = e.detail.value.gender_chosed;
      var telephone = e.detail.value.telephone;
      var email = e.detail.value.email;
    if (!nickname){
      wx.showToast({
        title: '姓名不能为空',
        icon:"none"
      })
      return false;
    } else if(!telephone){
      wx.showToast({
        title: '手机号不能为空',
        icon: "none"
      })
      return false;
    } else if (!gender_chosed){
      wx.showToast({
        title: '性别不能为空',
        icon: "none"
      })
      return false;
    } else if (!email) {
      wx.showToast({
        title: '邮箱不能为空',
        icon: "none"
      })
      return false;
    }else{
      wx.request({
        url: util.huodong_baoming,
        data: {
          huodong_id: that.data.huodong_id,
          openid: app.globalData.openid,
          name: nickname,
          tel: telephone,
          sex: gender_chosed,
          email: email,
        },
        success: function (res) {
        },
        fail: function () {

        }
      })

      wx.showToast({
        title: '提交成功',
        icon: "none"
      })
      wx.navigateTo({
        url: '/pages/signStatus/signStatus?huodongid'+that.data.huodongid,
      })
     
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})